package com.example.gybicycle.common.entity;


import jodd.util.StringUtil;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 *  返回实体
*/
@Data
@Accessors(chain = true)
//@Schema(description = "响应类")
//@Schema(name =description="响应类")
public class Result<T> {
    /**
     *  状态
    */
//    @Schema(description ="响应码",example="200成功 401失败")
//    @Schema(description = value ="响应码",example="200成功 401失败")
    private Integer code;
    /**
     *  消息
    */
//    @Schema(description ="消息")
    private String msg;

    /**
     *  内容
    */
//    @Schema(description ="结果集")
    private T data;
    //    成功 状态
    public static final Integer SUCCESS_SATE=200;
    //    保存、查询失败 状态
    public static final Integer FAIL_SATE=401;
    //    唯一键冲突 状态
    public static final Integer ONEKEY_SATE=402;
//    验证码错误或失效
    public static final Integer CODE_FAIL_SATE=402;
    //    登录失效
    public static final Integer LOGIN_SATE=201;
    //登录失败
    public static final Integer LOGIN_FAIL=202;
    //    报错 状态
    public static final Integer ERROR_SATE=500;
    //    数据验证失败 状态
    public static final Integer DATA_FAIL_SATE=501;
    //    成功 返回原因
    public static final String SUCCESS_MSG="success";
    //    保存、查询失败 状态
    public static final String FAIL_MSG="fail";
    //    未登录 消息
    public static final String LOGIN_MSG="Login failed";
    public static final String LOGIN_FAIL_MSG="Login failed";
    //    报错 状态
    public static final String ERROR_MSG="error";
    //    数据验证失败 状态
    public static final String DATA_FAIL_MSG="Data validation failed";
    public static final String CODE_FAIL_MSG="验证码错误或失效";
    public static final int MEMBER_NO_REGISTER_STAE= 202;
    public static final String MEMBER_NO_REGISTER_MSG="账号未注册";
    /** 成功 */
    public static <T> Result<T> success(T data){
        Result<T> result=new Result<>();
        result.setMsg(SUCCESS_MSG);
        result.setCode(SUCCESS_SATE);
        result.setData(data);
        return result;
    }
    public static <T> Result<T> noRegister(T data){
        Result<T> result=new Result<>();
        result.setMsg(MEMBER_NO_REGISTER_MSG);
        result.setCode(MEMBER_NO_REGISTER_STAE);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> reData(int num,T data){
        Result<T> result=new Result<>();
        if(num>0){
            result.setMsg(SUCCESS_MSG);
            result.setCode(SUCCESS_SATE);
            result.setData(data);
        }else {
            result.setMsg(FAIL_MSG);
            result.setCode(FAIL_SATE);
        }
        return result;
    }
    public static <T> Result<T> success(T data,String msg){
        Result<T> result=new Result<>();
        result.setMsg(msg);
        result.setCode(SUCCESS_SATE);
        result.setData(data);
        return result;
    }
    public static <T> Result<T>  oneKey(String msg){
        Result result=new Result<>();
        result.setCode(ONEKEY_SATE);
//        result.setData(msg);
        result.setMsg(msg);
        return result;
    }
  /*  public static <T> Result<T> success(String msg){
        Result<T> result=new Result<>();
        if(StringUtil.isEmpty(msg))
            msg=SUCCESS_MSG;
        result.setMsg(msg);
        result.setCode(SUCCESS_SATE);
        return result;
    }*/
    public static <T> Result<T> fail(){
        Result<T> result =new Result<>();
        result.setMsg(FAIL_MSG);
        result.setCode(FAIL_SATE);
        return result;
    }
    public static <T> Result<T> fail(String error){
        Result<T> result =new Result<>();
        result.setMsg(error);
        result.setCode(FAIL_SATE);
        return result;
    }
    public static <S> Result<String> fail(String msg,String data){
        Result<String> result =new Result<>();
        result.setMsg(msg);
        result.setData(data);
        result.setCode(FAIL_SATE);
        return result;
    }
    public static <T> Result<T> codefail(String error){
        Result<T> result =new Result<>();
        if(error!=null)
            result.setMsg(error);
        else result.setMsg(CODE_FAIL_MSG);
        result.setCode(CODE_FAIL_SATE);
        return result;
    }
    public static <T> Result<T> codefail(){
        Result<T> result =new Result<>();
        result.setMsg(CODE_FAIL_MSG);
        result.setCode(CODE_FAIL_SATE);
        return result;
    }
    public static <T> Result<T> loginOut(){
        Result<T> result =new Result<>();
        result.setMsg(LOGIN_MSG);
        result.setCode(LOGIN_SATE);
        return result;
    }
    public static <T> Result<T> error(){
        Result<T> result =new Result<>();
        result.setMsg(ERROR_MSG);
        result.setCode(ERROR_SATE);
        return result;
    }
    public static <T> Result<T> error(String error){
        Result<T> result =new Result<>();
        if(error==null)
            error="空指针错误";
        result.setMsg(error);
        result.setCode(ERROR_SATE);
        return result;
    }
    public static <T> Result<String> error(String error,String data){
        Result<String> result =new Result<>();
        if(error==null)
            error="空指针错误";
        result.setMsg(error);
        result.setData(data);
        result.setCode(ERROR_SATE);
        return result;
    }
    public static <T> Result<T> noAccess(String msg){
        Result<T> result = new Result<>();
        result.setMsg(msg);
        result.setCode(403);
        return result;
    }
    public static <T> Result<T> noPower(){
        Result<T> result = new Result<>();
        result.setMsg("无此权限");
        result.setCode(405);
        return result;
    }
}
